package com.tinckay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinckay.common.exception.ServiceException;
import com.tinckay.common.util.LocalDateTimeUtils;
import com.tinckay.common.util.ShiroUtils;
import com.tinckay.entity.dto.StorehousePlanDto;
import com.tinckay.entity.form.StorehousePlanForm;
import com.tinckay.entity.po.Storehouse;
import com.tinckay.entity.po.StorehousePlan;
import com.tinckay.entity.po.Task;
import com.tinckay.mapper.StorehousePlanMapper;
import com.tinckay.mapper.TaskMapper;
import com.tinckay.service.StorehousePlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仓体浇筑计划 服务实现类
 * </p>
 *
 * @author Jean
 * @since 2018-11-06
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class StorehousePlanServiceImpl extends ServiceImpl<StorehousePlanMapper, StorehousePlan> implements StorehousePlanService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private StorehouseServiceImpl storehouseService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Serializable id) {
        StorehousePlan storehousePlan = baseMapper.selectById(id);
        updateStorehouse(storehousePlan.getStoreCode(), 0);
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(StorehousePlan storehousePlan) {
        final Integer row = baseMapper.selectCount(new QueryWrapper<StorehousePlan>().lambda().eq(StorehousePlan::getStoreCode, storehousePlan.getStoreCode()));
        if (row > 0) {
            throw new ServiceException("该仓体已有绑定浇筑计划");
        }

        Integer maxPourNo = baseMapper.selectMaxPourNo(storehousePlan.getPlanTime());
        if (null != maxPourNo) {
            storehousePlan.setPourNo(maxPourNo + 1);
        }
        storehousePlan.setStoreState(100);
        updateStorehouse(storehousePlan.getStoreCode(), 1);
        return super.save(storehousePlan);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(StorehousePlan storehousePlan) {
        //TODO: 修改浇筑日期，改变浇筑序号
        storehousePlan.setStoreCode(null);
        storehousePlan.setPourNo(null);
        storehousePlan.setPlanTime(null);
        StorehousePlan exist = baseMapper.selectById(storehousePlan.getId());
        int addFlag = 100;
        if (addFlag != exist.getStoreState()) {
            throw new ServiceException("浇筑计划状态不允许修改");
        }
        return storehousePlan.updateById();
    }

    private void updateStorehouse(String storeCode, int state) {
        Storehouse storehouse = storehouseService.getOne(new QueryWrapper<Storehouse>().lambda().eq(Storehouse::getCode, storeCode));
        if (null != storehouse) {
            storehouse.setState(state);
            storehouse.updateById();
            storehouseService.cacheAndSendMsg();
        }
    }

    @Override
    public List<StorehousePlan> findList(String barnNo, String pumpCode, String planTime, Integer storeState) {
        final List<StorehousePlan> list = baseMapper.findList(barnNo, pumpCode, planTime, storeState);
        return list;
    }

    @Override
    public void changePourNo(long planId, int adjustPourNo) {
        StorehousePlan storehousePlan = baseMapper.selectById(planId);
        if (null == storehousePlan) {
            throw new ServiceException("该浇筑计划不存在");
        }
        LocalDate planTime = storehousePlan.getPlanTime();
        Integer maxPourNo = baseMapper.selectMaxPourNo(planTime);
        Integer srcPourNo = storehousePlan.getPourNo();
        if (adjustPourNo == srcPourNo) {
            return;
        }
        if (srcPourNo.equals(maxPourNo) && adjustPourNo >= maxPourNo) {
            return;
        }
        //调整序号大于原序号
        if (adjustPourNo > srcPourNo) {
            baseMapper.subPourNo(adjustPourNo, planTime);
        } else {
            //调整序号小于原序号
            baseMapper.addPourNo(adjustPourNo, planTime);
        }
        storehousePlan.setPourNo(adjustPourNo);
        storehousePlan.updateById();
    }

    @Override
    public List<Map<String, Object>> countPlan(List<String> storeCode) {
        return baseMapper.countPlan(storeCode);
    }

    @Override
    public StorehousePlan storehousePlanInfo(String storeCode) {
        return baseMapper.storehousePlanInfo(storeCode);
    }

    @Override
    public List<Map<String, Object>> statistical(int statisticalFlag, String startPlanTime, String endPlanTime) {
        if (StrUtil.isBlank(endPlanTime)) {
            endPlanTime = LocalDateTimeUtils.currDate();
        }
        return baseMapper.statistical(statisticalFlag, startPlanTime, endPlanTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StorehousePlan buildStorehousePan(StorehousePlanForm form) {
        Storehouse storehouse = new Storehouse();
        storehouse.setPipeCode(form.getPipeCode());
        storehouse.setType(Byte.valueOf(form.getType().toString()));
        storehouse.setLength(form.getLength());
        storehouse.setWidth(form.getWidth());
        storehouse.setLeftWidth(form.getLeftWidth());
        storehouse.setRightWidth(form.getLeftWidth() == 0 ? 0 : form.getWidth() - form.getLeftWidth());
        storehouse.setHeight(form.getHeight());
        storehouse.setRightHeight(form.getRightHeight());
        storehouse.setHeiDifference(form.getType() == 0 ? form.getHeight() : form.getRightHeight() - form.getHeight());
        //storehouse.setWallThickness();
        storehouse.setCubic(form.getCubic());
        storehouse.setAbnormityCubic(form.getAbnormityCubic());
        storehouse.setVentHeight(form.getVentHeight());
        storehouse.setState(1);

        storehouseService.save(storehouse);

        StorehousePlan storehousePlan = new StorehousePlan();
        storehousePlan.setStoreState(100);
        storehousePlan.setStoreCode(storehouse.getCode());
        storehousePlan.setPumpCode(form.getPumpCode());
        storehousePlan.setPlanTime(LocalDate.now());
        storehousePlan.setLimitHeight(form.getLimitHeight());
        storehousePlan.setStopHeight(form.getStopHeight());
        storehousePlan.setPumpCode(form.getPumpCode());

        save(storehousePlan);
        return storehousePlan;
    }

}
